const express = require('express');
const router = express.Router();
const db = require('../../utils/db');

// 药品销售明细
router.post('/DrugSalesGet',(req,res)=>{
    const {page, pageSize,date,name,type}=req.body;
    let sql=``
if(type=='2'){
    sql=`SELECT * FROM indirect  LEFT JOIN medicinal_information ON indirect.i_m_id=medicinal_information.m_id LEFT JOIN prescription_record ON indirect.i_re_id=prescription_record.re_id WHERE (m_name LIKE '%${name}%' OR re_id LIKE '%${name}%' ) AND re_createTime LIKE '%${date}%'  LIMIT ${(page - 1)* pageSize},${pageSize}`;
}else{
    sql=`SELECT * FROM indirect  LEFT JOIN medicinal_information ON indirect.i_m_id=medicinal_information.m_id LEFT JOIN prescription_record ON indirect.i_re_id=prescription_record.re_id  WHERE (m_name LIKE '%${name}%' OR re_id LIKE '%${name}%' ) AND re_createTime LIKE '%${date}%' AND re_type LIKE '%${type}%' LIMIT ${(page - 1)* pageSize},${pageSize}`;
}
let  sql1=`SELECT COUNT(*) AS total FROM indirect  LEFT JOIN medicinal_information ON indirect.i_m_id=medicinal_information.m_id LEFT JOIN prescription_record ON indirect.i_re_id=prescription_record.re_id  WHERE re_createTime LIKE '%${date}%' `;
    db.query(sql,(err,data)=>{
    if(err){
        res.send({ error: 1, msg: err });
    }else{
        db.query(sql1,(err2,data2)=>{
            if(err2){
                res.send({ error: 1, msg: err2 });
            }else{
                res.send({ error: 0, data: data, total: data2[0].total });
            }
        })
    }
    })
})

module.exports = router